package cn.com.infcn.monitor.secure;

import org.apache.mina.core.filterchain.IoFilterAdapter;
import org.apache.mina.core.session.IoSession;


public class SecurityFilter extends IoFilterAdapter {
	
//	private final static Logger logger = LoggerFactory.getLogger(SecurityFilter.class);
	
	/*protected boolean checkSecutiry(IoSession session, RequestMessage req) {
		int fsize = req.getAllFields().size();
		if (req.hasExtension(Request.loginRequest)) {
			if (fsize == 2) 
				return true;
			else {
				logger.debug("Login request has {} extensions", fsize);
				return false;
			}
		}
		
		 FIXME:
		if (req.hasExtension(Request.carReturnDataRequest) || 
				req.hasExtension(Request.ddCouponListRequest) ||
				req.hasExtension(Request.routeReviewRequest) ||
				req.hasExtension(Request.poiReviewRequest)) {
			return true;
		}
		
		SecretTokenMgr mgr = SecretTokenMgr.getInstance();
		String token = (String)session.getAttribute(SecretTokenMgr.USER_SESSION_KEY);
		if (token != null) {
			logger.debug("Found token: {} in session", token);
			mgr.contain(token);
			return true;
		}
		else {
			token = req.getAccessToken();
			logger.debug("Try to find token from SecretTokenMgr, RequestMessage token: {}", token);
			if (mgr.contain(token)) {
				UserInfo u = (UserInfo)mgr.getSession(token);
				session.setAttribute(SecretTokenMgr.USER_ID_KEY, u.getUserId());
				session.setAttribute(SecretTokenMgr.USER_SESSION_KEY, token);

				return true;
			}
		}
		
		return false;
	}*/

	@Override
	public void messageReceived(NextFilter nextFilter, IoSession session, Object message) throws Exception {
//		RequestMessage request = (RequestMessage)message;
		nextFilter.messageReceived(session, message);
		/*if (checkSecutiry(session, request)) {
			nextFilter.messageReceived(session, message);
		}
		else {
			logger.warn("User - {}:{} loggin failed", 
					MdcInjectionFilter.getProperty(session, "remoteIp"), 
					MdcInjectionFilter.getProperty(session, "remotePort"));
			
			ResponseMessage response = ResponseMessage.newBuilder().
					setStatusCode(StatusCode.AUTHZ_FAIL).build();
			if (session != null) {
	    		session.write(response);
	        }
		}*/
	}
	
}
